package com.loongxun.playwithyou.ui.activity.playuser;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.loongxun.playwithyou.ApiInterface;
import com.loongxun.playwithyou.R;
import com.loongxun.playwithyou.bean.Order;
import com.loongxun.playwithyou.bean.SendRequest;
import com.loongxun.playwithyou.network.MyHttpClientRespenseHandler;
import com.loongxun.playwithyou.network.nHttpClient;
import com.loongxun.playwithyou.util.ToastView;

import org.json.JSONObject;

/**
 * 陪玩人员结算明细详情
 * Created by Administrator on 2017/8/8.
 */
public class BillInfoActivity extends Activity {

    private Context context;
    private SendRequest send;
    private String statisId,orderNo;
    private Order order = new Order();

    //标题
    private TextView tv_top_title;

    private TextView tv_nm_item_order_no,tv_nm_item_start_time,tv_nm_item_hours_num, tv_nm_item_type,
            tv_nm_item_rate,tv_nm_item_service_charge,
            tv_nm_item_prive,tv_nm_item_address, tv_nm_item_address_hint, tv_nm_item_phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_order_info);
        context = BillInfoActivity.this;

        statisId = getIntent().getStringExtra("statisId");
        orderNo = getIntent().getStringExtra("orderNo");

        try {
            send = new SendRequest();
            send.statisId = statisId;
            send.orderNo = orderNo;
            //判断此会员是否为陪玩人员
            nHttpClient.getAll(ApiInterface.BILL_IRDER_DETAIL, send.toBillInfoJson().toString(), orderMemberHandler);
        }catch (Exception e){
            ToastView toast = new ToastView(context, "参数错误");
            toast.show();
        }
        initView();
    }

    private void initData() {
        tv_nm_item_order_no.setText("订  单  号："+order.getOrder_no());
        tv_nm_item_start_time.setText("预约时间："+order.getPlay_start_time());
        tv_nm_item_hours_num.setText("预约时长：共计" +order.getHours_num()+"小时");
        tv_nm_item_type.setText("订单金额：￥"+order.getOrder_all_price());

        tv_nm_item_rate.setText("费率："+order.getPlatform_coef());
        tv_nm_item_service_charge.setText("手续费：￥"+order.getPlatform_amount());

        tv_nm_item_prive.setText("结算金额：￥"+order.getSettlement_amount());
        tv_nm_item_address.setText("陪练地址："+order.getAddressIntro());
        tv_nm_item_address_hint.setText(order.getAddressDetail());
        tv_nm_item_phone.setText("客户电话："+order.getUser_phone());
    }

    private void initView() {
        tv_top_title = (TextView) findViewById(R.id.tv_top_title);
        tv_top_title.setText("明细详情");
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        findViewById(R.id.ll_user_info).setVisibility(View.GONE);
        findViewById(R.id.ll_nm_item_rate).setVisibility(View.VISIBLE);
        findViewById(R.id.ll_nm_item_order).setVisibility(View.VISIBLE);

        tv_nm_item_order_no = (TextView) findViewById(R.id.tv_nm_item_order_no);
        tv_nm_item_start_time = (TextView) findViewById(R.id.tv_nm_item_start_time);
        tv_nm_item_hours_num = (TextView) findViewById(R.id.tv_nm_item_hours_num);
        tv_nm_item_type = (TextView) findViewById(R.id.tv_nm_item_type);

        tv_nm_item_rate = (TextView) findViewById(R.id.tv_nm_item_rate);
        tv_nm_item_service_charge = (TextView) findViewById(R.id.tv_nm_item_service_charge);

        tv_nm_item_prive = (TextView) findViewById(R.id.tv_nm_item_prive);
        tv_nm_item_address = (TextView) findViewById(R.id.tv_nm_item_address);
        tv_nm_item_address_hint = (TextView) findViewById(R.id.tv_nm_item_address_hint);
        tv_nm_item_phone = (TextView) findViewById(R.id.tv_nm_item_phone);
    }

    private MyHttpClientRespenseHandler orderMemberHandler = new MyHttpClientRespenseHandler() {

        @Override
        public void success(JSONObject json) {
            Integer succeed = json.optJSONObject("status").optInt("succeed", 0);
            if (succeed == 1) {
                try {
                    JSONObject orderObject = json.getJSONObject("data").getJSONObject("playOrder");
                    order.setOrder_no(orderObject.getString("order_no"));
                    order.setPlay_start_time(orderObject.getString("play_start_time"));
                    order.setHours_num(orderObject.getInt("hours_num"));
                    order.setOrder_all_price(orderObject.getDouble("order_all_price"));
                    order.setUser_phone(orderObject.getString("user_phone"));

                    String address = json.getJSONObject("data").getString("merchant");
                    if (!address.equals("null")) {
                        JSONObject addressObject = json.getJSONObject("data").getJSONObject("merchant");
                        order.setAddressIntro(addressObject.getString("addressIntro"));
                        order.setAddressDetail(addressObject.getString("addressDetail"));
                    }else {
                        order.setAddressIntro("");
                        order.setAddressDetail("");
                    }

                    String bill = json.getJSONObject("data").getString("billOrder");
                    if (!bill.equals("null")) {
                        JSONObject userObject = json.getJSONObject("data").getJSONObject("billOrder");
                        order.setPlatform_amount(userObject.getString("platform_amount"));
                        order.setSettlement_amount(userObject.getString("settlement_amount"));
                        order.setPlatform_coef(userObject.getString("platform_coef"));
                    }else {
                        order.setPlatform_amount("");
                        order.setSettlement_amount("");
                        order.setPlatform_coef("");
                    }
                    initData();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else {
                String error_desc = json.optJSONObject("status").optString("error_desc");
                new ToastView(context, error_desc).show();
            }
        }

        @Override
        public void faile(String json) {
            new ToastView(context, json).show();
        }
    };

}
